package cn.edu.scau.cmi.wangmaoren.client.classicsDesignPattern;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import af.javafx.layout.AfLinearLayout;
import cn.edu.scau.cmi.wangmaoren.singleton.Chairman;
import cn.edu.scau.cmi.wangmaoren.singleton.Marshal;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SingletonClient extends Application
{
	private AfLinearLayout root = new AfLinearLayout();
	private AfLinearLayout line = new AfLinearLayout();
	private Button singletonButton=new Button("单例模式创建");
	private Button multitionButton=new Button("多例模式创建");
	private Label blankLeft = new Label();
	private Label blankRight = new Label();
	private TextField nameText = new TextField();
	private TextArea inforText=new TextArea();
	
	@Override
	public void start(Stage primaryStage)
	{
		try
		{
			initLayout();//初始化界面
			
			Scene scene = new Scene(root, 750, 463);
			primaryStage.setTitle("单例/多例模式");
			primaryStage.setScene(scene);
			primaryStage.show();
			
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		launch(args);
	}
	
	public void openWindow()
	{
		initLayout();//初始化界面
		
		Scene scene = new Scene(root,750,463);
		Stage stage = new Stage();
		stage.setTitle("单例/多例模式");
		stage.setScene(scene);
		stage.show();
	}
	
	//单例模式创建
	private void creatSingleton()
	{
	    String name=nameText.getText();
		
		if(!name.isEmpty())
		{
			nameText.clear();
			
			if(!Chairman.isExist())
			{
				Chairman chairman=Chairman.getInstance(name);
				inforText.appendText(chairman.getName()+"  "+chairman+"\n");
			}
			else
			{
				Chairman chairman=Chairman.getInstance(name);
				inforText.appendText("\n"+"创建 "+name+" 对象失败，已有该类的单例，无法再创建新的对象，此时获取的对象是："+"\n");
				inforText.appendText(chairman.getName()+"  "+chairman+"\n");
			}
			
		}
		
	}
	
	//多例模式创建
	private void creatMultition()
	{
		String name=nameText.getText();
		List <Marshal> marshals=new ArrayList<>();
		
		if(!name.isEmpty())
		{
			nameText.clear();
			
			if(isMarshalExist(marshals,name))
			{
				inforText.appendText("\n"+"创建 "+name+" 对象失败，已有该名的元帅存在"+"\n");
			}
			else
			{
				if(!Marshal.isFull())
				{
					
					marshals = Marshal.addInstance(name);
					inforText.appendText(marshals.get(marshals.size()-1).getName()+"  "+marshals.get(marshals.size()-1)+"\n");
				}
				else
				{
					
					inforText.appendText("\n"+"创建 "+name+" 对象失败，已有10个元帅，无法再创建新的对象"+"\n");
				}
			}
		}
		
	}
	
	private boolean isMarshalExist(List <Marshal> marshals,String name)
	{
		Iterator<Marshal> iter = marshals.iterator();
		while(iter.hasNext())
		{
			Marshal m = iter.next();
			if(m.getName().equals(name))
			{
				return true;
			}
		}
		
		return false;
	}
	
	/*
	 * 初始化界面
	 */
	public void initLayout()
	{
		root.orientation(Orientation.VERTICAL).spacing(20).padding(20);
		line.spacing(20);
		
		line.getChildren().addAll(blankLeft,nameText, singletonButton,multitionButton,blankRight);
		line.layout("1 242px 180px 180px 1");
		root.add(line, "50px");
		root.add(inforText,"1");
		
		nameText.setPromptText("请输入主席/元帅名字");
		singletonButton.setFont(Font.font ("华文中宋", 20));
		multitionButton.setFont(Font.font ("华文中宋", 20));
		nameText.setFont(Font.font ("华文中宋", 19));
		inforText.setFont(Font.font ("华文中宋", 18));
		
		Image singletonImage = new Image(getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/singleton/chairman.png"));
		singletonButton.setGraphic(new ImageView(singletonImage));
		Image multitionImage = new Image(getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/singleton/marshal.png"));
		multitionButton.setGraphic(new ImageView(multitionImage));
		
		singletonButton.setOnAction((e) -> {
			creatSingleton();
		});
		multitionButton.setOnAction((e) -> {
			creatMultition();
		});
		
	}
}
